/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ToDoMainWindow.java
 *
 * Created on 1 May, 2011, 11:51:41 AM
 */
package view;

import action.ConvertToSpeech;
import database.actions.DatabaseOperations;
import action.MyDateListener;
import database.models.Tasks;
import java.util.ArrayList;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import model.MyTableModel;
import org.freixas.jcalendar.JCalendarCombo;

/**
 *
 * @author Dhawal
 */
public class ToDoMainWindow extends javax.swing.JFrame {

    private JCalendarCombo calenderCombobox;
    private MyDateListener dateListener;
    private static ArrayList<Tasks> taskList = new ArrayList<Tasks>();
    private Tasks tasks;
    private ConvertToSpeech convertToSpeech = new ConvertToSpeech();

    public Tasks getTasks() {
        return tasks;
    }

    public void setTasks(Tasks tasks) {
        this.tasks = tasks;
    }

    public static JTable getTaskTable() {
        return taskTable;
    }

    /** Creates new form ToDoMainWindow */
    public ToDoMainWindow() {
        initComponents();
        initDateCombo();

        calenderCombobox.setEnabled(false);
        detailsPanel.setVisible(false);
        taskTable.getTableHeader().setReorderingAllowed(false);
        addTaskList();
    }

    public static void addTaskList() {
        taskList.clear();
        Object[] records = DatabaseOperations.getRecords("Tasks");
        for (Object object : records) {
            Tasks tasks = (Tasks) object;
            taskList.add(tasks);
        }
        MyTableModel model = new MyTableModel(taskList);
        taskTable.setModel(model);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        taskTable = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        AddButton = new javax.swing.JButton();
        SearchButton = new javax.swing.JButton();
        listenButton = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();
        detailsPanel = new javax.swing.JPanel();
        deleteButton = new javax.swing.JButton();
        modifyButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        detailsTitleTextBox = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        detailsTaskDescriptionTextArea = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        detailsCreationDate = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        detailsDueDateComboPanel = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        detailsStatusCombo = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        detailsPriorityCombo = new javax.swing.JComboBox();
        jMenuBar1 = new javax.swing.JMenuBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        taskTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        taskTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                taskTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(taskTable);

        jToolBar1.setRollover(true);

        AddButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/add_icon.png"))); // NOI18N
        AddButton.setToolTipText("Add New Task");
        AddButton.setFocusable(false);
        AddButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        AddButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        AddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(AddButton);

        SearchButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/search_icon.png"))); // NOI18N
        SearchButton.setToolTipText("Search Tasks");
        SearchButton.setFocusable(false);
        SearchButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        SearchButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        SearchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SearchButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(SearchButton);

        listenButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/listen_icon.png"))); // NOI18N
        listenButton.setToolTipText("Listen Tasks");
        listenButton.setFocusable(false);
        listenButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        listenButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        listenButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listenButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(listenButton);

        exitButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit_icon.png"))); // NOI18N
        exitButton.setToolTipText("Exit");
        exitButton.setFocusable(false);
        exitButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        exitButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(exitButton);

        detailsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Details", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        deleteButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/delete_icon.gif"))); // NOI18N
        deleteButton.setText("Delete");
        deleteButton.setToolTipText("Delete the selected task");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        modifyButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/modify_icon.png"))); // NOI18N
        modifyButton.setText("Modify");
        modifyButton.setToolTipText("Modify Task");
        modifyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modifyButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Title : ");

        detailsTitleTextBox.setEditable(false);
        detailsTitleTextBox.setText(" ");

        jLabel2.setText("Task Description : ");

        detailsTaskDescriptionTextArea.setColumns(20);
        detailsTaskDescriptionTextArea.setEditable(false);
        detailsTaskDescriptionTextArea.setRows(5);
        jScrollPane2.setViewportView(detailsTaskDescriptionTextArea);

        jLabel3.setText("Creation Date : ");

        detailsCreationDate.setEditable(false);

        jLabel4.setText("Due Date : ");

        detailsDueDateComboPanel.setLayout(new java.awt.BorderLayout());

        jLabel5.setText("Status : ");

        detailsStatusCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "incomplete", "complete", "in mid" }));
        detailsStatusCombo.setEnabled(false);

        jLabel6.setText("Priority : ");

        detailsPriorityCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5" }));
        detailsPriorityCombo.setEnabled(false);

        javax.swing.GroupLayout detailsPanelLayout = new javax.swing.GroupLayout(detailsPanel);
        detailsPanel.setLayout(detailsPanelLayout);
        detailsPanelLayout.setHorizontalGroup(
            detailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(detailsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(detailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(detailsPanelLayout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(74, 74, 74)
                        .addComponent(detailsTitleTextBox, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
                        .addContainerGap(104, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(detailsPanelLayout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addContainerGap(452, Short.MAX_VALUE))
                    .addGroup(detailsPanelLayout.createSequentialGroup()
                        .addGroup(detailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel6))
                        .addGap(18, 18, 18)
                        .addGroup(detailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(detailsPanelLayout.createSequentialGroup()
                                .addComponent(deleteButton)
                                .addGap(18, 18, 18)
                                .addComponent(modifyButton)
                                .addContainerGap())
                            .addGroup(detailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(detailsPriorityCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(detailsPanelLayout.createSequentialGroup()
                                    .addGroup(detailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(detailsStatusCombo, javax.swing.GroupLayout.Alignment.LEADING, 0, 231, Short.MAX_VALUE)
                                        .addComponent(detailsDueDateComboPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, detailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(detailsCreationDate)
                                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)))
                                    .addGap(156, 156, 156)))))))
        );
        detailsPanelLayout.setVerticalGroup(
            detailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(detailsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(detailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(detailsTitleTextBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addGroup(detailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(detailsPanelLayout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(detailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(detailsCreationDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(detailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(detailsPanelLayout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jLabel4))
                    .addGroup(detailsPanelLayout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(detailsDueDateComboPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(detailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(detailsStatusCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(detailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(detailsPriorityCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addGroup(detailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(deleteButton)
                    .addComponent(modifyButton))
                .addContainerGap())
        );

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 519, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 519, Short.MAX_VALUE)
            .addComponent(detailsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(detailsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-527)/2, (screenSize.height-611)/2, 527, 611);
    }// </editor-fold>//GEN-END:initComponents

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        // TODO add your handling code here:
        int confirm = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete this task?", "Confirm", JOptionPane.YES_NO_OPTION);
        if (confirm == 0) {
            DatabaseOperations.deleteTask(detailsTitleTextBox.getText());
            JOptionPane.showMessageDialog(this, "Task deleted successfully", "success", JOptionPane.INFORMATION_MESSAGE);
            ToDoMainWindow.addTaskList();
            taskTable.updateUI();
            detailsPanel.setVisible(false);
        }
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_exitButtonActionPerformed

    private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed
        showNewTaskDialog();
    }

    private void showNewTaskDialog() {
        JDialog.setDefaultLookAndFeelDecorated(true);
        NewTaskWindow newTask = new NewTaskWindow(this, true);
        newTask.setVisible(true);
    }//GEN-LAST:event_AddButtonActionPerformed

    private void taskTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_taskTableMouseClicked
        // TODO add your handling code here:
        if (taskTable.getSelectedRowCount() == 1) {
            detailsPanel.setVisible(true);
            showDetails();

        } else {
            detailsPanel.setVisible(false);
            this.pack();
        }
    }//GEN-LAST:event_taskTableMouseClicked

    private void SearchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SearchButtonActionPerformed
        // TODO add your handling code here:
        JDialog.setDefaultLookAndFeelDecorated(true);
        SearchDialogWindow dialogWindow = new SearchDialogWindow(this, true);
        dialogWindow.setVisible(true);
    }//GEN-LAST:event_SearchButtonActionPerformed

    private void modifyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifyButtonActionPerformed
        JDialog.setDefaultLookAndFeelDecorated(true);
        ModifyTaskWindow modifyWin = new ModifyTaskWindow(this, true, getTasks());
        modifyWin.setVisible(true);
}//GEN-LAST:event_modifyButtonActionPerformed

    private void listenButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listenButtonActionPerformed
        // TODO add your handling code here:
        String speechText="";
        for (Tasks tasks1 : taskList) {
            speechText=speechText+"Task "+tasks1.getTaskId()+tasks1.getTitle()+"Due By "+tasks1.getDueBy();
        }
        convertToSpeech.toSpeech(speechText);
    }//GEN-LAST:event_listenButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ToDoMainWindow().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddButton;
    private javax.swing.JButton SearchButton;
    private javax.swing.JButton deleteButton;
    private javax.swing.JTextField detailsCreationDate;
    private javax.swing.JPanel detailsDueDateComboPanel;
    private javax.swing.JPanel detailsPanel;
    private javax.swing.JComboBox detailsPriorityCombo;
    private javax.swing.JComboBox detailsStatusCombo;
    private javax.swing.JTextArea detailsTaskDescriptionTextArea;
    private javax.swing.JTextField detailsTitleTextBox;
    private javax.swing.JButton exitButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton listenButton;
    private javax.swing.JButton modifyButton;
    private static javax.swing.JTable taskTable;
    // End of variables declaration//GEN-END:variables

    private void showDetails() {
        int[] rowCounts = taskTable.getSelectedRows();
        if (rowCounts.length == 1) {
            String titleValue = (String) taskTable.getValueAt(rowCounts[0], 2);
            Tasks taskRecord = DatabaseOperations.getTaskRecord(titleValue);
            setTasks(taskRecord);

            calenderCombobox.setDate(taskRecord.getDueBy());
            detailsCreationDate.setText("" + taskRecord.getCreationDate());
            detailsPriorityCombo.setSelectedItem(taskRecord.getPriority());
            detailsStatusCombo.setSelectedItem(taskRecord.getStatus());
            detailsTaskDescriptionTextArea.setText(taskRecord.getTaskDescription());
            detailsTitleTextBox.setText(taskRecord.getTitle());

        } else {
            detailsPanel.setVisible(false);
        }
    }

    private void initDateCombo() {
        dateListener = new MyDateListener();
        calenderCombobox = new JCalendarCombo(JCalendarCombo.DISPLAY_DATE, false);
        calenderCombobox.addDateListener(dateListener);

        detailsDueDateComboPanel.add(calenderCombobox);
    }
}
